package com.yaoran.userauth.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yaoran.userauth.dao.UserDao;
import com.yaoran.userauth.entity.User;
import com.yaoran.userauth.service.UserService;
import com.yaoran.userauth.utils.PageUtils;
import com.yaoran.userauth.utils.Query;
import com.yaoran.userauth.utils.ThreadLocalUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    private final static Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private UserDao userDao;

    @Override
    public void addUser(User user, Date createTime){
        if (createTime != null){
            user.setCreateTime(createTime);
        }else{
            user.setCreateTime(new Date());
        }
        user.setUpdateTime(new Date());
        String name = ThreadLocalUtils.get("operate");
        user.setCreateBy(name);
        userDao.addUser(user);
    }

    @Override
    public PageUtils<User> listUser(Query query,String keyWord){
        Page<User> page = PageHelper.startPage(query.getPage(),query.getLimit());
        List<User> list = userDao.listUser(keyWord);
        int total = (int) page.getTotal();
        return new PageUtils(list,total,query.getLimit(),query.getPage());
    }

    @Override
    public void updateUser(User user){
        user.setUpdateTime(new Date());
        String name = ThreadLocalUtils.get("operate");
        user.setUpdateBy(name);
        userDao.updateUser(user);
    }

    @Override
    public void deleteUser(Long id){
        userDao.deleteUser(id);
    }

    @Override
    public void deleteManyUser(Long[] ids){
        userDao.deleteManyUsers(ids);
    }

    @Override
    public User getUser(Long id){
        return userDao.getUser(id);
    }
}
